package com.example.project_last.controller;

import com.example.project_last.entity.Dept;
import com.example.project_last.entity.Result;
import com.example.project_last.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.project_last.entity.Iclass;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author cyt
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/iclass")
public class ClassController {
    @Autowired
    private IClassService classService;
    @Autowired
    private IClassService iClassService;

    //请求部门数据
    @GetMapping
    public Result<List> list(){
        List<Iclass> list = classService.list();
        return Result.success(list);
    }

    // 根据ID查询部门
    @GetMapping("/{id}")
    public Result<Iclass> getById(@PathVariable Long id) {
        Iclass iclass = classService.getById(id);
        if ( iclass!= null) {
            return Result.success(iclass);
        }
        return Result.fail("班级不存在");
    }

    //新增部门
    @PostMapping
    public Result add(@RequestBody Iclass iclass){
        iclass.setCreateTime(LocalDateTime.now());
        iclass.setUpdateTime(LocalDateTime.now());
        boolean save = classService.save(iclass);
        if(save) {
            return Result.success();
        }
        return Result.fail("新增班级失败，请稍后再试");
    }

    //修改部门
    @PutMapping
    public Result update(@RequestBody Iclass iclass){
        iclass.setUpdateTime(LocalDateTime.now());
        boolean update=iClassService.updateById(iclass);
        if(update) {
            return Result.success();
        }
        return Result.fail("修改班级失败，请稍后再试！");
    }

    //删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        boolean remove=iClassService.removeById(id);
        if(remove) {
            return Result.success();
        }
        return Result.fail("删除班级失败，请稍后再试");
    }


}
